global parity
global G
global H
global errTable
global n, 
global k
global n_k;

parity = [0 1 1; 
          1 0 1; 
          1 1 1; 
          1 1 0 ];
G = [eye(4,4)  parity];
H = [parity; eye(3,3)];

function table = syndtable2(h)
//SYNDTABLE Produce syndrome decoding table.
//   T = SYNDTABLE(H) returns a binary matrix that represents the first column of
//   a standard array of a linear code having a parity-check matrix H.  T
//   consists of coset leaders ordered sequentially by the associated syndromes
//   such that the first row contains an error pattern with a syndrome equal to
//   0, and the last row contains an error pattern with a syndrome equal to
//   2^(number of rows in H)-1.

if isempty(h)
    error('The parity check matrix cannot be empty.');
end

[n_k,n]=size(h);                    // n_k denotes n-k
if n_k >= n | ~isequal(size(size(h)),[1 2]),
    error('There must be more columns than rows in the parity check matrix.');
end

if  max(h) < 0 | max(h) > 1 | int(h) ~= h then
    error('The parity check matrix must contain only binary numbers.');
end

table = zeros(2^n_k,n);
//emptyRows = [2:2^n_k]';

// Each row contains a single-error pattern
// (except last row which is all-zeros)
E = flipud([zeros(1, n); eye(n, n)]);

// Each row contains syndrome of a single-error pattern
// (except last row)
synSingleErr = bi2de(fliplr(modulo(E*h',2)));

[a,b] = unique(synSingleErr);
//b = fliplr(b);
table(synSingleErr(b)+1,:) = E(b,:);

endfunction

errTable = syndtable(H');
errTable2 = syndtable2(H');
errTable == errTable2
  

